﻿using Microsoft.EntityFrameworkCore;
using Abp.Zero.EntityFrameworkCore;
using Sunlit.Business.Authorization.Roles;
using Sunlit.Business.Authorization.Users;
using Sunlit.Business.MultiTenancy;
using Sunlit.Business.Regions;
using Sunlit.Business.Policies;
using Sunlit.Business.Enterprises;
using Sunlit.Business.Commerces;
using Sunlit.Business.Serves;
using Sunlit.Business.HomeSliders;
using Sunlit.Business.Wishlists;

namespace Sunlit.Business.EntityFrameworkCore
{
    public class BusinessDbContext : AbpZeroDbContext<Tenant, Role, User, BusinessDbContext>
    {
        /* Define a DbSet for each entity of the application */
        /// <summary>
        /// 地区表
        /// </summary>
        public DbSet<Region> Regions { get; set; }
        /// <summary>
        /// 政策咨询类别
        /// </summary>
        public DbSet<PolicyCate> PolicyCates { get; set; }
        /// <summary>
        /// 政策咨询
        /// </summary>
        public DbSet<Policy> Policies { get; set; }
        /// <summary>
        /// 政策咨询留言
        /// </summary>
        public DbSet<PolicyMessage> PolicyMessages { get; set; }
        /// <summary>
        /// 企业选址类别
        /// </summary>
        public DbSet<EnterpriseCate> EnterpriseCates { get; set; }
        /// <summary>
        /// 企业选址
        /// </summary>
        public DbSet<Enterprise> Enterprises { get; set; }
        /// <summary>
        /// 企业选址留言
        /// </summary>
        public DbSet<EnterpriseMessage> EnterpriseMessages { get; set; }
        /// <summary>
        /// 商业选址类别
        /// </summary>
        public DbSet<CommerceCate> CommerceCates { get; set; }
        /// <summary>
        /// 商业选址
        /// </summary>
        public DbSet<Commerce> Commerces { get; set; }
        /// <summary>
        /// 商业选址留言
        /// </summary>
        public DbSet<CommerceMessage> CommerceMessages { get; set; }
        /// <summary>
        /// 企业服务类别
        /// </summary>
        public DbSet<ServeCate> ServeCates { get; set; }
        /// <summary>
        /// 企业服务
        /// </summary>
        public DbSet<Serve> Serves { get; set; }
        /// <summary>
        /// 幻灯片
        /// </summary>
        public DbSet<HomeSlider> HomeSliders { get; set; }
        /// <summary>
        /// 收藏列表
        /// </summary>
        public DbSet<Wishlist> Wishlists { get; set; }

        public BusinessDbContext(DbContextOptions<BusinessDbContext> options)
            : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            // 地区自关联关系
            modelBuilder.Entity<Region>(r =>
            {
                r.HasMany(a => a.ChildRegions).WithOne(a => a.ParentRegion).HasForeignKey(a => a.ParentId);
            });

            // 政策咨询类别自关联关系
            modelBuilder.Entity<PolicyCate>(r =>
            {
                r.HasMany(p => p.Children).WithOne(p => p.Parent).HasForeignKey(p => p.ParentId);
            });
            // 企业选址类别自关联关系
            modelBuilder.Entity<EnterpriseCate>(r =>
            {
                r.HasMany(p => p.Children).WithOne(p => p.Parent).HasForeignKey(p => p.ParentId);
            });
            // 商业选址类别自关联关系
            modelBuilder.Entity<CommerceCate>(r =>
            {
                r.HasMany(p => p.Children).WithOne(p => p.Parent).HasForeignKey(p => p.ParentId);
            });
            // 企业服务类别自关联关系
            modelBuilder.Entity<ServeCate>(r =>
            {
                r.HasMany(p => p.Children).WithOne(p => p.Parent).HasForeignKey(p => p.ParentId);
            });
        }
    }
}
